package com.junxian.demo.read;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSON;
import com.junxian.demo.model.VActiveAlarmVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

/**
 * 读取 excel 表格 内容
 */
public class EasyExcelRead {

    private final static Logger logger = LoggerFactory.getLogger(EasyExcelRead.class);

    public static void main(String[] args) {
        simpleRead();
    }

    /**
     * @description 简单读取 (同步读取)
     * @author sunjx
     * @date 2020/9/28 17:40
     * @param
     * @return void
     **/
    private static void simpleRead() {
        // 读取 excel 表格的路径
        String readPath = "D:\\bigData\\活动告警列表.xlsx";
        try {
            // sheetNo --> 读取哪一个 表单
            // headLineMun --> 从哪一行开始读取( 不包括定义的这一行，比如 headLineMun为2 ，那么取出来的数据是从 第三行的数据开始读取 )
            // clazz --> 将读取的数据，转化成对应的实体，需要 extends BaseRowModel
            Sheet sheet = new Sheet(1, 1, VActiveAlarmVO.class);
            List<Object> readList = EasyExcelFactory.read(new FileInputStream(readPath), sheet);
            List<VActiveAlarmVO> list = new ArrayList<>();
            for (Object obj : readList) {
                list.add((VActiveAlarmVO) obj);
            }
            logger.info("查看VActiveAlarmVO结果集" + JSON.toJSONString(list));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
